﻿Public Class Manager
#Region " Events "
    Public Shared Event JobStarting(ByVal job As Entities.Backup.Job)
    Public Shared Event JobStartingGUI(ByVal job As Entities.Backup.Job)
    Friend Shared Sub OnJobStarting(ByVal job As Entities.Backup.Job)
        RaiseEvent JobStarting(job)
        Framework.Threading.Invoke(AddressOf OnJobStarting_Callback, job)
    End Sub
    Private Shared Sub OnJobStarting_Callback(ByVal job As Entities.Backup.Job)
        RaiseEvent JobStartingGUI(job)
    End Sub
    Public Shared Event JobCompleted(ByVal job As Entities.Backup.Job)
    Public Shared Event JobCompletedGUI(ByVal job As Entities.Backup.Job)
    Friend Shared Sub OnJobCompleted(ByVal job As Entities.Backup.Job)
        RaiseEvent JobCompleted(job)
        Framework.Threading.Invoke(AddressOf OnJobCompleted_Callback, job)
    End Sub
    Private Shared Sub OnJobCompleted_Callback(ByVal job As Entities.Backup.Job)
        RaiseEvent JobCompletedGUI(job)
    End Sub
    Public Shared Event JobInformation(ByVal job As Entities.Backup.Job, ByVal information As String)
    Public Shared Event JobInformationGUI(ByVal job As Entities.Backup.Job, ByVal information As String)
    Friend Shared Sub OnJobInformation(ByVal job As Entities.Backup.Job, ByVal information As String)
        RaiseEvent JobInformation(job, information)
        Framework.Threading.Invoke(AddressOf OnJobInformation_Callback, job, information)
    End Sub
    Private Shared Sub OnJobInformation_Callback(ByVal job As Entities.Backup.Job, ByVal information As String)
        RaiseEvent JobInformationGUI(job, information)
    End Sub
    Public Shared Event JobException(ByVal job As Entities.Backup.Job, ByVal exception As Exception)
    Public Shared Event JobExceptionGUI(ByVal job As Entities.Backup.Job, ByVal exception As Exception)
    Friend Shared Sub OnJobException(ByVal job As Entities.Backup.Job, ByVal exception As Exception)
        RaiseEvent JobException(job, exception)
        Framework.Threading.Invoke(AddressOf OnJobException_Callback, job, exception)
    End Sub
    Private Shared Sub OnJobException_Callback(ByVal job As Entities.Backup.Job, ByVal exception As Exception)
        RaiseEvent JobExceptionGUI(job, Exception)
    End Sub

    Public Class PackagingDaemonActivityEventArgs
        Inherits System.EventArgs

        Public FilesInQueue As Integer
        Public Job As Entities.Backup.Job
        Public FilePath As String
        Public FileSize As Long
        Public Action As String
        Public ActionCompletedPercent As Short
        Public ActionBytesProcessed As Long
    End Class
    Public Shared Event PackagingDaemonActivityGUI(ByVal e As PackagingDaemonActivityEventArgs)
    Friend Shared Sub OnPackagingDaemonActivity(ByVal e As PackagingDaemonActivityEventArgs)
        Framework.Threading.Invoke(AddressOf OnPackagingDaemonActivity_Callback, e)
    End Sub
    Private Shared Sub OnPackagingDaemonActivity_Callback(ByVal e As PackagingDaemonActivityEventArgs)
        RaiseEvent PackagingDaemonActivityGUI(e)
    End Sub


    Public Class AnalyzeAndFindChangesActivityEventArgs
        Inherits System.EventArgs

        Public JobsInQueue As Integer
        Public Job As Entities.Backup.Job
        Public CurrentDirectoryPath As String
        Public FilesAnalyzed As Integer
        Public ChangedFilesFound As Integer
    End Class
    Public Shared Event AnalyzeAndFindChangesActivityGUI(ByVal e As AnalyzeAndFindChangesActivityEventArgs)
    Friend Shared Sub OnAnalyzeAndFindChangesActivity(ByVal e As AnalyzeAndFindChangesActivityEventArgs)
        Framework.Threading.Invoke(AddressOf OnAnalyzeAndFindChangesActivity_Callback, e)
    End Sub
    Private Shared Sub OnAnalyzeAndFindChangesActivity_Callback(ByVal e As AnalyzeAndFindChangesActivityEventArgs)
        RaiseEvent AnalyzeAndFindChangesActivityGUI(e)
    End Sub

    Private Shared mJobStatusSyncObj As New Object
    Friend Shared Sub UpdateJobStatus(ByVal job As Entities.Backup.Job)
        SyncLock mJobStatusSyncObj
            Dim udtNew As Entities.Backup.JobStatus = Entities.Backup.JobStatus.Idle
            If Handler_AnalyzeAndFindChanges.IsJobActive(job) Then
                udtNew = Entities.Backup.JobStatus.Running
            ElseIf Handler_PackagingDaemon.IsJobActive(job) Then
                udtNew = Entities.Backup.JobStatus.Running
            End If
            EntityHandlers.Backup.Jobs.SetNewStatus(job, udtNew)
        End SyncLock
    End Sub
    Public Shared Sub AbortJob(ByVal job As Entities.Backup.Job)
        Handler_AnalyzeAndFindChanges.AbortJob(job)
        Handler_PackagingDaemon.AbortJob(job)
        UpdateJobStatus(job)
        OnJobInformation(job, "Job was aborted by user")
    End Sub

#End Region

    Public Shared Sub Start()
        Framework.Net.Manager.RegisterCommand(New Commands.ContractUpdate)
        Handler_ScheduleWatcher.Start()
        Handler_AnalyzeAndFindChanges.Start()
        Handler_PackagingDaemon.Start()
    End Sub
    Public Shared Sub [Stop](ByVal message As Framework.Message)
        message.SetMessage("Shutting down backup daemons")
        Handler_PackagingDaemon.Stop()
        Handler_AnalyzeAndFindChanges.Start()
        Handler_ScheduleWatcher.Stop()
    End Sub

    Friend Shared Sub OnLogException(ByVal exception As Exception)
        Framework.Misc.HandleException(exception)
    End Sub

    Public Shared Function IsFileB2BBackupfile(ByVal filePath As String) As Boolean
        Return Handler_PackagingDaemon.IsFileB2BBackupfile(filePath)
    End Function
    Public Shared Function IsFileEncrypted(ByVal filePath As String) As Boolean
        Return Handler_PackagingDaemon.IsFileEncrypted(filePath)
    End Function
    Public Shared Function GetBackedUpFileContainerInfo(ByVal filePath As String, ByVal passwordIfNeeded As String) As Entities.Backup.BackedUpFileContainerInfo
        Return Handler_PackagingDaemon.GetBackedUpFileContainerInfo(filePath, passwordIfNeeded)
    End Function
Public Shared Function StartRestoreTask(ByVal request As RestoreRequest) As RestoreTask
    Return Handler_Restore.StartNew(request)
End Function

End Class
